package com.iccaplayer;

import java.util.ArrayList;

import com.example.iccaplayer.R;
import com.example.iccaplayer.R.layout;
import com.example.iccaplayer.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class ConnInsActivity extends Activity {
	private static final String TAG = "ConnInsActivity";
	
	public static final int TOTAL_INSTRUMENTS = 20;
	
	Intent mainIntent;
	Bundle extras;
	
	String insType;
	int capablePos[];
	String capableIns[];
	ArrayList<String> insNameList = new ArrayList<String>();
	ArrayAdapter<String> insNameAdapter;
	int deviceId;
//	int nSearched, nConnected;
	String preference, manufacturer, model, version, description;

	TableLayout infoTable;
	TableRow infoRow[] = new TableRow[TOTAL_INSTRUMENTS];	
	TextView preferenceText[] = new TextView[TOTAL_INSTRUMENTS];
	TextView manufacturerText[] = new TextView[TOTAL_INSTRUMENTS];
	TextView modelText[] = new TextView[TOTAL_INSTRUMENTS];
	TextView versionText[] = new TextView[TOTAL_INSTRUMENTS];
	TextView descriptionText[] = new TextView[TOTAL_INSTRUMENTS];
	Spinner insNameSpin[] = new Spinner[TOTAL_INSTRUMENTS];
//	Spinner insNameSpin;

	int connectedPos;
	
	Button okBtn, cancelBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_conn_ins);
		
		infoTable = (TableLayout)findViewById(R.id.InfoTable);
		okBtn = (Button)findViewById(R.id.ConnOkBtn);
		cancelBtn = (Button)findViewById(R.id.ConnCancelBtn);
			
		mainIntent = getIntent();
		extras = mainIntent.getExtras();
//		nConnected = 0;
				
		// Get Information and Edit Name of Instrument
		if(extras != null){
			Log.d(TAG, "here");
//			nSearched = extras.getInt("NSearched");
//			nConnected = nSearched;
			deviceId = extras.getInt("DeviceId");
			capablePos = extras.getIntArray("CapablePosArray");
			capableIns = extras.getStringArray("CapableInsArray");
			preference = extras.getString("Preference");
			manufacturer = extras.getString("Manufacturer");
			model = extras.getString("Model");
			version = extras.getString("Version");
			description = extras.getString("Description");		
			
			int index = 0;
			while(capableIns[index] != null){
				insNameList.add(capableIns[index]);
				index++;
			}
			insNameAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, insNameList);

//			for(int boardPos=0; boardPos<nSearched; boardPos++){
				createLayout(0);
//			}			
		
			// Close Information of Instrument Activity
			okBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					getSelectedItem();
					
//					mainIntent.putExtra("NConnected", nConnected);
					mainIntent.putExtra("ConnectedPos", connectedPos);
					mainIntent.putExtra("DevicePos", deviceId);
					setResult(RESULT_OK, mainIntent);
					finish();
				}
			});		
			
			// Close Information of Instrument Activity
			cancelBtn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					setResult(RESULT_CANCELED);
					finish();
				}
			});
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.conn_ins, menu);
		return true;
	}
	
	private void createLayout(int pos){
		int index, boardPos;
		index= 0;
		boardPos = pos;		
		
		// Create Information Board of Searched ArduIns
		infoRow[boardPos] = new TableRow(this);
		
		preferenceText[boardPos] = new TextView(this);  
		preferenceText[boardPos].setWidth(150);
		preferenceText[boardPos].setGravity(Gravity.CENTER_HORIZONTAL);
		preferenceText[boardPos].setText(preference);
		infoRow[boardPos].addView(preferenceText[boardPos]);
		
		manufacturerText[boardPos] = new TextView(this);
		manufacturerText[boardPos].setWidth(150);
		manufacturerText[boardPos].setGravity(Gravity.CENTER_HORIZONTAL);
		manufacturerText[boardPos].setText(manufacturer);		
		infoRow[boardPos].addView(manufacturerText[boardPos]);
		
		modelText[boardPos] = new TextView(this);
		modelText[boardPos].setWidth(100);
		modelText[boardPos].setGravity(Gravity.CENTER_HORIZONTAL);
		modelText[boardPos].setText(model);		
		infoRow[boardPos].addView(modelText[boardPos]);
		
		versionText[boardPos] = new TextView(this);
		versionText[boardPos].setWidth(100);
		versionText[boardPos].setGravity(Gravity.CENTER_HORIZONTAL);
		versionText[boardPos].setText(version);		
		infoRow[boardPos].addView(versionText[boardPos]);
		
		descriptionText[boardPos] = new TextView(this);
		descriptionText[boardPos].setWidth(200);
		descriptionText[boardPos].setGravity(Gravity.CENTER_HORIZONTAL);
		descriptionText[boardPos].setText(description);		
		infoRow[boardPos].addView(descriptionText[boardPos]);		
		
		// Create Spinner of Being Able To Connect with ArduIns
		insNameSpin[boardPos] = new Spinner(this);	
		insNameSpin[boardPos].setGravity(Gravity.CENTER);
		insNameSpin[boardPos].setId(TOTAL_INSTRUMENTS*boardPos);
		insNameSpin[boardPos].setAdapter(insNameAdapter);
		insNameSpin[boardPos].setSelection(boardPos);
		insNameSpin[boardPos].setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {		
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}				
		});	
		infoRow[boardPos].addView(insNameSpin[boardPos]);
		
		infoTable.addView(infoRow[boardPos]);
	}
	
	private void getSelectedItem(){
		int position;
		position = -1;
		
//		for(int boardPos=0; boardPos<nConnected; boardPos++){
			position = insNameSpin[0].getSelectedItemPosition();
			connectedPos = position;
//		}		
	}
}